(function () {
    const del = document.querySelectorAll(".me-delete"),
        add_stu = document.querySelector("#add-stu"),
        upload_stu = document.querySelector("#upload-stu"),
        open_add = document.querySelector("#open-add"),
        open_upload = document.querySelector("#open-upload"),
        model_background = document.querySelector(".model-background"),
        close_model = document.querySelectorAll("#close-model"),
        downOption = document.querySelector("#login-user"),
        editor = document.querySelectorAll("i.fa.fa-edit"),
        user_option = document.querySelector("#user-option"),
        my_file = upload_stu.querySelector("#my-file"),
        file_input = upload_stu.querySelector(".file-input"),
        list_file = upload_stu.querySelector(".list-file"),
        reset = upload_stu.querySelector("#reset"),
        submit = upload_stu.querySelector("#submit");

    my_file.onclick = () => {
        file_input.click();
    }

    submit.onclick = e => {
        if (file_input.value == "") {
            alert("您还没有添加文件哦");
            e.preventDefault()
        }
    }

    file_input.onchange = e => {
        let fileName = e.target.files[0].name
        if (fileName.length > 12) {
            let spliteName = fileName.split('.');
            fileName = spliteName[0].substring(0, 10) + "... ." + spliteName[1];
        }
        const template = `<div class="item">
                        <span><i class="fa fa-file-excel-o"></i></span>
                        <span>${fileName} · 处理中</span>
                    </div>`
        list_file.innerHTML = template
    }

    reset.onclick = () => {
        if (file_input.value == "") {
            alert("您还没有添加文件哦")
        } else {
            list_file.childNodes[0].remove()
        }
    }
    editor.forEach(item => {
        let target = 0;
        item.addEventListener("click", function () {
            let inputNode = this.previousElementSibling;
            if (target === 0) {
                inputNode.style.height = "100%";
                inputNode.style.width = "95%";
                inputNode.style.border = ".14rem solid var(--main)";
                inputNode.disabled = false;
                this.className = "fa fa-close";
                target = 1;
            } else {
                inputNode.style.height = "100%";
                inputNode.style.width = "95%";
                inputNode.style.border = "none";
                inputNode.disabled = true;
                this.className = "fa fa-edit";
                target = 0;
            }
        })
    })

    open_add.onclick = () => {
        model_background.style.display = "block";
        add_stu.style.display = "block";
        add_stu.style.height = "400px";
    }

    open_upload.onclick = () => {
        model_background.style.display = "block";
        upload_stu.style.display = "block";
        upload_stu.style.height = "300px";
    }

    close_model.forEach(item => {
        item.onclick = function () {
            this.parentNode.parentNode.parentNode.style.display = "none";
            this.parentNode.parentNode.style.display = "none";
        }
    })

    del.forEach(option => {
        option.addEventListener("click", delStudent)
    })

    function delStudent() {
        let pageSize = 8,
            totalPage = parseInt(this.dataset.allpage),
            current = parseInt(this.dataset.current),
            allStudent = parseInt(this.dataset.allstu);
        if (allStudent % pageSize === 1) {
            if (current === totalPage) {
                current--;
            }
        }
        if (confirm("确定删除吗？")) {
            $("#dataStu").load(`${this.dataset.content}/student?action=delete`, {
                'id': this.dataset.id.trim(),
                'current': current
            }, function (responseTxt, statusText, xhr) {
            });
        }
    }


    downOption.addEventListener("mouseover", function () {
        user_option.classList.add("active")
    })
    downOption.addEventListener("mouseout", function () {
        user_option.classList.remove("active");
    })

    /**
     * 提交按钮
     * @param id
     * @param stuId
     */

    $('table .me-upload').click(function () {
        $("#dataStu").load(`${this.dataset.content}/student?action=update`, {
            'id': this.dataset.id.trim(),
            'name': this.parentNode.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.childNodes[1].value.trim(),
            'sex': this.parentNode.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.childNodes[1].value.trim(),
            'stu_id': this.dataset.code.trim(),
            'age': this.parentNode.previousElementSibling.previousElementSibling.previousElementSibling.previousElementSibling.childNodes[1].value.trim(),
            'height': this.parentNode.previousElementSibling.previousElementSibling.previousElementSibling.childNodes[1].value.trim(),
            'weight': this.parentNode.previousElementSibling.previousElementSibling.childNodes[1].value.trim(),
            'hobby': this.parentNode.previousElementSibling.childNodes[1].value.trim(),
            'current': this.dataset.current
        }, function (responseTxt, statusText, xhr) {
        })
    });
}(window, document))